package Help.Manager.Album;


import java.util.ArrayList;

import photos.core.commons.Contants;


import com.example.easynote.AlbumActivity;
import com.example.easynote.R;


import DTO.AlbumModel;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;


/*
 * AlbumsFragment lÃ  Fragment hiá»ƒn thá»‹ Grid gá»“m táº¥t cáº£ Album.
 */
@SuppressLint("ValidFragment")
public class AlbumsFragment extends Fragment implements OnItemClickListener{
	public static final int MIN_COL_WIDTH = 130;
	public static final int SPACE_WIDTH = 12;
	public static final int MARGIN = 24;
	
	ArrayList<AlbumModel> albums;
	AlbumsAdapter adapter;
	float columnWidth;
	
	public AlbumsFragment() {
		
	}
	
	public AlbumsFragment(ArrayList<AlbumModel> albums) {
		this.albums = albums;
	}
	
	@Override
	public View onCreateView(
			LayoutInflater inflater, 
			ViewGroup container,
			Bundle savedInstanceState) {
		View layout = inflater.inflate(R.layout.fragment_albums, null);
		
		GridView grid = (GridView)layout.findViewById(R.id.gridview_albums);
		
		Activity activity = getActivity();
		activity.registerForContextMenu(grid);
		
		grid.setColumnWidth((
				int) LayoutHelpers.getColumnWidth(
						activity, 
						MIN_COL_WIDTH, 
						SPACE_WIDTH, 
						MARGIN));
		
		this.adapter = new AlbumsAdapter(getActivity(), this.albums, grid, this.columnWidth);
		grid.setAdapter(this.adapter);
		
		grid.setOnItemClickListener(this);
		
		return layout;
	}
	
	public void update() {
		this.adapter.notifyDataSetChanged();
	}

	
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    /*
     * (non-Javadoc)
     * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
     * Báº¯t sá»± liá»‡n lá»±a chá»�n Album trÃªn GridView.
     * Chuyá»ƒn tiáº¿p Ä‘áº¿n AlbumActivity.
     */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(getActivity(), AlbumActivity.class);
		
		AlbumModel selected = this.albums.get(position);
		intent.putExtra(Contants.AlbumPath, selected.getPath());
		
		startActivity(intent);
	}
}
